package com.vercon.commonmirror.manage;

import com.vercon.commonmirror.mode.user.UserInfoData;

import java.util.ArrayList;
import java.util.List;

/**
 * 单例管理用户类
 */
public class UserManager {

    private static UserManager userManager = null;
    private List<UserInfoData> users = null;

    public static UserManager getInstance() {
        if (userManager == null) {
            synchronized (UserManager.class) {
                if (userManager == null) {
                    userManager = new UserManager();
                }
                return userManager;
            }
        } else {
            return userManager;
        }
    }

    private UserManager() {
        init();
    }

    private void init() {
        users = new ArrayList<>();
    }

    /**
     * init the user
     */
    public void setUser(List<UserInfoData> user) {
        if (users == null) return;
        users.clear();
        users.addAll(user);
    }

    /**
     * has user info
     */
    public List<UserInfoData> getUsers() {
        if (users == null) return null;
        return users;
    }


    /**
     * has user info
     */
    public UserInfoData getUser(int position) {
        if (users == null || users.size() == 0) return null;
        return users.get(position);
    }

    /**
     * has user info
     */
    public UserInfoData getUser(String userId) {
        if (users == null || users.size() == 0) return null;
        for (UserInfoData userInfo : users) {
            if (userInfo.get_id().equals(userId)) {
                return userInfo;
            }
        }
        return null;
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public boolean hasLogined() {
        return users != null && users.size() != 0;
    }

    /**
     * remove the user info
     */
    public void removeUser() {
        if (users == null) return;
        users.clear();
    }


}
